package com.zxb;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Function {
    static List<Student> list = new ArrayList<>(Arrays.asList(//创建一个可变的列表。
            new Student(111, "张三", 55, "北京"),
            new Student(222, "李四", 66, "上海"),
            new Student(333, "王五", 44, "广州"),
            new Student(444, "赵六", 88, "深圳")
    ));
    public static Scanner scanner = new Scanner(System.in);

    // 添加学生（使用 getIndex 检查学号重复）
    public static void addStudent() {
        System.out.println("添加学生");
        int id;
        while (true) {
            System.out.print("请输入学生编号：");
            id = scanner.nextInt();
            if (getIndex(id) == -1) break;
            System.out.println("该id已存在，请重新输入！");
        }
        System.out.print("请输入学生姓名：");
        String name = scanner.next();
        System.out.print("请输入学生年龄：");
        int age = scanner.nextInt();
        System.out.print("请输入学生地址：");
        String address = scanner.next();
        list.add(new Student(id, name, age, address));
        System.out.println("添加成功！");
    }

    // 删除学生（通过学号检查存在性）
    public static void deleteStudent() {
        System.out.println("删除学生");
        if (list.isEmpty()) {
            System.out.println("无学生信息！");
            return;
        }
        System.out.print("请输入要删除的学号：");
        int id = scanner.nextInt();
        int index = getIndex(id);
        if (index == -1) System.out.println("学号不存在！");
        else {
            list.remove(index);
            System.out.println("删除成功！");
        }
    }

    // 修改学生信息
    public static void updateStudent() {
        System.out.println("修改学生");
        if (list.isEmpty()) {
            System.out.println("无学生信息！");
            return;
        }
        System.out.print("请输入要修改的学号：");
        int id = scanner.nextInt();
        int index = getIndex(id);
        if (index == -1) System.out.println("学号不存在！");
        else {
            Student s = list.get(index);
            System.out.print("请输入新年龄：");
            s.setAge(scanner.nextInt());
            System.out.print("请输入新地址：");
            s.setAddress(scanner.next());
            System.out.println("修改成功！");
        }
    }
    //通过id查询单个学生信息
    public static void personalStudent() {
        System.out.println("查询学生");
        if (list.isEmpty()) {
            System.out.println("无学生信息！");
            return;
        }
        int id = scanner.nextInt();
        int index = getIndex(id);
        if (index == -1) System.out.println("学号不存在！");
        else {
            Student s = list.get(index);
            System.out.println("id\t\t姓名\t\t年龄\t\t地址");
            System.out.printf(s.getId() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "岁" + "\t" + s.getAddress() + "\n");
        }

    }
    // 查询所有学生
    public static void queryStudent() {
        System.out.println("查询所有学生");
        if (list.isEmpty()) {
            System.out.println("无学生信息！");
            return;
        }
        System.out.println("id\t\t姓名\t\t年龄\t\t地址");
        for (Student s : list) {
            System.out.printf(s.getId() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "岁" + "\t" + s.getAddress() + "\n");
        }
    }

    // 辅助方法：根据学号查找索引
    private static int getIndex(int id) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() == id) return i;
        }
        return -1;//未找到
    }
}